/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QDebug>
#include "descarcarepreturimodel.h"

DescarcarePreturiModel::DescarcarePreturiModel(QObject* parent, Query * qry):
	QAbstractTableModel(parent), m_query(qry)
{
	m_query=defaultDatabase()->query("preturiPozitiiIntrariObiecteInventar");
	m_cantitate=defaultDatabase()->query("pozitiiIesiriObiecteInventar")->value("cantitate").toDouble();
	qreal cantitate=m_cantitate;
	for (int i=0;i<m_query->size();i++)
		if (cantitate>m_query->result()[i].value("cantitate_ramasa").toDouble())
		{
			m_cantitati.push_back( m_query->result()[i].value("cantitate_ramasa").toDouble());
			cantitate-=m_query->result()[i].value("cantitate_ramasa").toDouble();
		}
		else
		{
			m_cantitati.push_back( cantitate );
			cantitate=0;
		}
}

int DescarcarePreturiModel::columnCount(const QModelIndex& /*parent*/) const 
{
	return 3;
}

int DescarcarePreturiModel::rowCount(const QModelIndex& /*parent*/) const 
{
	return m_query->result().size();
}

QVariant DescarcarePreturiModel::data(const QModelIndex& index, int role) const
{
	if (role != Qt::DisplayRole && role != Qt::EditRole)
		return QVariant();

	switch(index.column())
	{
		case 0:
			return m_query->result()[index.row()].value("data");
		case 1:
			return m_query->result()[index.row()].value("cantitate_ramasa");
		case 2:
			return m_cantitati[index.row()];
	}
	return QVariant();
}

bool DescarcarePreturiModel::setData( const QModelIndex & index, const QVariant & value, int role)
{
	qreal cantitate=value.toDouble();
	if (cantitate>m_cantitate)
		cantitate=m_cantitate;
	m_cantitati[index.row()]=cantitate;
	cantitate=m_cantitate-cantitate;
	for (int i=0;i<m_cantitati.size();i++)
	{
		if (i==index.row())
			continue;
		if (m_cantitati[i]>cantitate)
			m_cantitati[i]=cantitate;
		cantitate-=m_cantitati[i];
	}
	m_cantitati[0]+=cantitate;
	reset();
}

QFlags< Qt::ItemFlag > DescarcarePreturiModel::flags(const QModelIndex& index) const 
{
	if (index.column()<2)
		return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
	else
		return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable;
}

QVariant DescarcarePreturiModel::headerData(int section, Qt::Orientation orientation, int role) const 
{
	if (role != Qt::DisplayRole && role != Qt::EditRole && orientation != Qt::Vertical)
		return QVariant();

	switch(section)
	{
		case 0:
			return "Data achizitie";
		case 1:
			return "Cantitate";
		case 2:
			return "Cantitata ce se descarca";
	}
	return QVariant();
}
